/**
 * dbms-sjtu
 * Record.java created at Aug 13, 2011
 */
package dbms.record;

import java.util.ArrayList;
import java.util.List;

import dbms.Constants;

/**
 * one record format
 * Record : rowId dataByteNum  recHeadSize type1 type2 type3 ... typeN data1 data2 data3 ... dataN
 * @author weapon
 */
public class Record {
	public int recordLength;
	public int dataByteNum;
	public int rowId;
	public int recHeadSize;  // N * INT_SIZE
	
	/**
	 * Integer type, Object value
	 * key: type, value: data
	 */
	//public Map<Integer, Object> datas = new HashMap<Integer, Object>();
	
	public List<Object> data = new ArrayList<Object>();
	public List<Integer> types = new ArrayList<Integer>();
	
	public Record(int rowId, List<Object> data){
		this.data = data;
		this.rowId = rowId;
		this.recHeadSize = data.size() * Constants.INT_SIZE;
		int pos = 0;
		/**
		 * typeValue 	type 	length
		 *   0			null	0
		 *   4			INT		4
		 *   N>5 odd 	VARCHAR (N-5)/2
		 */
		for(Object object : data){
			if(object instanceof Integer){
				types.add(Constants.INTEGER);
				pos += Constants.INT_SIZE;
			} else if(object instanceof Float){
				types.add(Constants.DECIMAL);
				pos += Constants.INT_SIZE;
			}else {
				types.add(object.toString().length() * 2 + 5);
				pos += object.toString().length();
			}
		}
		
		this.dataByteNum = pos;
		
		this.recordLength = (3 * Constants.INT_SIZE + this.dataByteNum + this.recHeadSize);
	}
}
